package com.moss.web;

import com.alibaba.fastjson2.JSONObject;
import com.moss.common.core.BaseController;
import com.moss.common.domain.AjaxResult;
import com.moss.common.domain.TreeEntity;
import com.moss.common.entity.SysMenuEntity;
import com.moss.system.service.MenuService;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author gxcc3
 * @date: 2024/8/26 16:44
 */
@RestController
@RequestMapping("menu")
public class MenuController extends BaseController {

    @Resource
    MenuService menuService;


    @GetMapping("/getTree")
    public AjaxResult<List<TreeEntity>> getTree(SysMenuEntity menu){
        List<SysMenuEntity> menus = menuService.selectMenuList(menu, getUserId());
        return AjaxResult.success(menuService.buildMenu2Tree(menus));
    }

    @GetMapping("/getRoleMenuTree/{roleId}")
    public AjaxResult<Map<String, Object>> getRoleMenuTree(@PathVariable Long roleId){
        List<SysMenuEntity> menus = menuService.selectMenuList(null,getUserId());
        Map<String,Object> result = new HashMap<>(2);
        result.put("checkedKeys", menuService.selectMenuIdListByRoleId(roleId));
        result.put("menus", menuService.buildMenu2Tree(menus));
        return AjaxResult.success(result);
    }


    @GetMapping("/list")
    public AjaxResult<List<SysMenuEntity>> getMenuList(SysMenuEntity menu){
        return AjaxResult.success(menuService.selectMenuList(menu, getUserId()));
    }

    @GetMapping("/{menuId}")
    public AjaxResult<SysMenuEntity> getMenuById(@PathVariable Long menuId){
        return AjaxResult.success(menuService.selectMenuById(menuId));
    }

    @PostMapping("/saveMenu")
    public AjaxResult<Void> saveMenu(@RequestBody SysMenuEntity menu){
        menu.setCreateBy(getUserId());
        menuService.saveMenu(menu);
        return AjaxResult.success();
    }

    @PostMapping("/updateMenu")
    public AjaxResult<Void> updateMenu(@RequestBody SysMenuEntity menu){
        menu.setUpdateBy(getUserId());
        menuService.updateMenu(menu);
        return AjaxResult.success();
    }

    @PostMapping("/deleteMenu")
    public AjaxResult<Void> deleteMenu(@RequestParam Long menuId){
        menuService.deleteMenu(menuId);
        return AjaxResult.success();
    }


}
